// index.js
import {getPhoneBooks,getSearchPhoneBooks} from '../util/request.js'
Page({
  data:{
    phoneBooks:[],
    keyword:'',
    active: 0
  },
  async onLoad(){    
    // const {data:res}= await wx.p.request({
    //   url: 'http://localhost:8081/phoneBook',
    //   method:'get'
    // })
    const res=await getPhoneBooks()    
    this.setData({
      phoneBooks:res
    })
    console.log(this.data.phoneBooks)
  },
  async onSearch() {
    console.log(this.data.keyword)
    // const {data:res}= await wx.p.request({
    //   url: 'http://localhost:8081/phoneBook/'+this.data.keyword,
    //   method:'get'
    // })
    const res=await getSearchPhoneBooks(this.data.keyword)
    this.setData({
      phoneBooks:res
    })
    console.log(this.data.phoneBooks)
  },
  async onClear() {
    const res=await getPhoneBooks()      
    this.setData({
      phoneBooks:res,
      keyword:''
    })
  },
  onChange(event) {
    if(this.data.active==event.detail)
      return
    if(event.detail==0){
      wx.navigateTo({
        url: '../index/index',
      })
    }else if(event.detail==1){
      wx.navigateTo({
        url: '../new/new',
      })
    }else{
      wx.navigateTo({
        url: '../initGroup/initGroup',
      })
    }
  }
})
